﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 二分查找._374.猜数字大小.简单
{
    class MainProject
    {
        static void Main(string[] args)
        {
            int[] array = new int[] { -1, 0, 3, 5, 9, 12 };

            //Console.WriteLine(ErFind(array, 9));
            Console.ReadKey();
        }
        public static int GuessNumber(int n)
        {

            #region 二分查找
            //思路：二分查找
            int left = 1, right = n;
            while (left <= right)
            {
                int mid = left + (right - left) / 2;
                int guessVal = Guess(mid);
                if (guessVal == 0) return mid;
                else if (guessVal == -1) right = mid - 1;
                else left = mid + 1;
            }
            return 0;
            #endregion
        }

        private static int Guess(int mid)
        {
            int val = 6;
            if (mid == val)
            {
                return 0;
            }
            else if(mid > val)
            {
                return -1;
            }
            else
            {
                return 1;
            }
        }
    }
}
